package sources;

import net.rim.device.api.system.AccelerometerSensor;
import net.rim.device.api.system.AccelerometerSensor.Channel;
import net.rim.device.api.system.Application;

public class AccelerometerSource extends RandomSource {

	private static final int X_OFF = 0;
	private static final int Y_OFF = 1;
	private static final int Z_OFF = 2;
	
	private Channel accelChannel = null;
	private short[] xyz = new short[3];
	
	public String getSourceName() {
		return "Accelerometer Source";
	}

	protected void generateNext() {
		accelChannel.getLastAccelerationData(xyz);
		addRawData(getInt());
		addToPools(getInt());
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private int getInt() {
		return (((xyz[X_OFF] & 0xFFFF) << 16) | (xyz[Y_OFF] & 0xFFFF)) ^ ((xyz[Z_OFF] & 0xFFFF) << 8);
	}

	protected void openResources() {
		accelChannel = AccelerometerSensor.openRawDataChannel(Application.getApplication());
	}

	protected void closeResources() {
		accelChannel.close();
	}

}
